package parser;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * @author 石小俊
 * @date 2024年03月29日 18:05
 */
public class DOMParser {
    public static void main(String[] args) {
        // 创建DOM工厂,通过静态工厂创建对象
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            // 获取解析器
            DocumentBuilder builder = factory.newDocumentBuilder();

            // 解析文档
            Document document = builder.parse(DOMParser.class.getClassLoader().getResourceAsStream("students.xml"));

            // 根据解析后的数据进行操作
            Element root = document.getDocumentElement();
            System.out.println("标签名:" + root.getTagName());
            System.out.println("no属性值:" + root.getAttribute("no"));
            // 获取所有属性
            NamedNodeMap attributes = root.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                // 获取当前迭代的属性
                Node node = attributes.item(i);
                System.out.println(node);
            }

            // 获取指定元素的所有子节点
            NodeList list = root.getChildNodes();
            System.out.println("子节点数量:" + list.getLength());
            for (int i = 0; i < list.getLength(); i++) {
                // 获取当前迭代的节点
                Node node = list.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
//                    System.out.println("这是元素节点");
                    // 如果是元素节点,则继续进行解析
                    // 先将当前节点对象转换成元素对象
                    Element element = (Element) node;
                    System.out.println("标签名:" + element.getTagName());
                    NamedNodeMap attrs = element.getAttributes();
                    for (int j = 0; j < attrs.getLength(); j++) {
                        Node item = attrs.item(j);
                        System.out.println(item);
                    }

                    NodeList childNodes = element.getChildNodes();
                    for (int j = 0; j < childNodes.getLength(); j++) {
                        Node item = childNodes.item(j);
                        if (item.getNodeType() == Node.ELEMENT_NODE) {
                            Element ele = (Element) item;
                            System.out.println("标签名:" + ele.getTagName());
                            System.out.println("姓名:" + ele.getTextContent());
                        }
                        if (item.getNodeType() == Node.TEXT_NODE) {

                        }
                    }
                }
                if (node.getNodeType() == Node.TEXT_NODE) {
//                    System.out.println("这是文本节点");
                    System.out.println("节点内容:"+node.getTextContent());
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
